/*
 * Copyright 2023-2085 xujin <ceo@itgrail.com>
 *
 * Licensed under the Grail License, Version 1.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * https://itgrail.com/docs?id=3&catalogId=87
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.itgrail.pilot.app.command.query;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itgrail.pilot.app.command.cmo.query.PageOKRCmd;
import com.itgrail.pilot.app.command.co.KeyResultDetailCO;
import com.itgrail.pilot.app.command.co.ObjectiveDetailCO;
import com.itgrail.pilot.app.command.co.OkrDetailCO;
import com.itgrail.pilot.app.command.co.OkrPeriodDetailCO;
import com.itgrail.pilot.app.converter.KeyResultClientConverter;
import com.itgrail.pilot.app.converter.ObjectiveClientConverter;
import com.itgrail.pilot.app.converter.OkrPeriodClientConverter;
import com.itgrail.pilot.domain.user.contant.OkrConstants;
import com.itgrail.pilot.infrastructure.tunnel.db.dao.ClassifyMapper;
import com.itgrail.pilot.infrastructure.tunnel.db.dao.KeyResultMapper;
import com.itgrail.pilot.infrastructure.tunnel.db.dao.ObjectiveMapper;
import com.itgrail.pilot.infrastructure.tunnel.db.dao.OkrPeriodMapper;
import com.itgrail.pilot.infrastructure.tunnel.db.dataobject.ClassifyDO;
import com.itgrail.pilot.infrastructure.tunnel.db.dataobject.KeyResultDO;
import com.itgrail.pilot.infrastructure.tunnel.db.dataobject.ObjectiveDO;
import com.itgrail.pilot.infrastructure.tunnel.db.dataobject.OkrPeriodDO;
import com.itgrail.pilot.infrastructure.tunnel.util.SessionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import com.itgrail.grail.cqrs.annotation.CmdHandler;
import com.itgrail.grail.cqrs.command.CommandExecutorI;
import com.itgrail.grail.dto.PageResult;
import com.itgrail.grail.dto.ResultData;
import com.itgrail.grail.exception.BusinessException;

import java.util.ArrayList;
import java.util.List;


@CmdHandler
public class PageOKRCmdExe implements CommandExecutorI<ResultData<PageResult<OkrDetailCO>>, PageOKRCmd> {
    @Autowired
    private OkrPeriodMapper okrPeriodMapper;

    @Autowired
    private ObjectiveMapper objectiveMapper;

    @Autowired
    private KeyResultMapper keyResultMapper;

    @Autowired
    private ObjectiveClientConverter objectiveClientConverter;

    @Autowired
    private KeyResultClientConverter keyResultClientConverter;

    @Autowired
    private OkrPeriodClientConverter okrPeriodClientConverter;

    @Autowired
    private ClassifyMapper classifyMapper;

    @Override
    public ResultData execute(PageOKRCmd cmd) {
        ResultData resultData = ResultData.success(null);
        check(cmd);
        resultData.setData(page(cmd));
        return resultData;
    }

    protected PageResult<OkrDetailCO> page(PageOKRCmd cmd) {
        //查询周期是否有效
        List<OkrDetailCO> resultDtos = new ArrayList<>();

        OkrPeriodDO okrPeriodDO = okrPeriodMapper.selectById(cmd.getOkrPeriodId());
        if (null == okrPeriodDO) {
            throw new BusinessException("500", "没有相关OKR周期");
        }
        OkrPeriodDetailCO okrPeriodDetailCO = okrPeriodClientConverter.dataToClient(okrPeriodDO);
        //查询当前周期下当前用户的所有okr
        Page<ObjectiveDO> page = new Page<>(cmd.getPageNo(), cmd.getPageSize());
        QueryWrapper<ObjectiveDO> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("deleted", OkrConstants.NOT_DELETED);
        if (null != SessionUtils.currentUserId()) {
            queryWrapper.eq("created", SessionUtils.currentUserId());
        }
        IPage<ObjectiveDO> iPage = objectiveMapper.selectPage(page, queryWrapper);
        if (null != iPage && !CollectionUtils.isEmpty(iPage.getRecords())) {
            iPage.getRecords().forEach(x -> {
                OkrDetailCO okrDetailCO = new OkrDetailCO();
                Integer classifyId = x.getClassifyId();
                ClassifyDO classifyDO = classifyMapper.selectById(classifyId);
                ObjectiveDetailCO objectiveDetailCO = objectiveClientConverter.dataToClient(x);
                objectiveDetailCO.setClassifyName(classifyDO.getName());
                okrDetailCO.setObjectiveDetailCO(objectiveDetailCO);
                //获取关键成果
                QueryWrapper<KeyResultDO> krWrapper = new QueryWrapper<>();
                krWrapper.eq("deleted", OkrConstants.NOT_DELETED);
                krWrapper.eq("objective_id", objectiveDetailCO.getId());
                List<KeyResultDO> keyResultDOS = keyResultMapper.selectList(krWrapper);
                List<KeyResultDetailCO> keyResultDetailCOS = keyResultClientConverter.dataToClients(keyResultDOS);
                okrDetailCO.setKeyResultDetailCO(keyResultDetailCOS);
                okrDetailCO.setOkrPeriodDetailCO(okrPeriodDetailCO);
                resultDtos.add(okrDetailCO);
            });
        }
        PageResult<OkrDetailCO> result = new PageResult<>();
        result.setList(resultDtos);
        result.setCurrentPage(iPage.getCurrent());
        result.setTotalCount(iPage.getTotal());
        result.setTotalPage(iPage.getPages());
        return result;
    }

    protected boolean check(PageOKRCmd cmd) {
        return true;
    }

}
